import os, sys

def _VmB(VmKey):
    '''Private.
    '''
    _proc_status = '/proc/%d/status' % os.getpid()
    _scale = {'kB': 1024.0, 'mB': 1024.0*1024.0,
          'KB': 1024.0, 'MB': 1024.0*1024.0}

    global _proc_status, _scale
     # get pseudo file  /proc/<pid>/status
    try:
        t = open(_proc_status)
        v = t.read()
        t.close()
    except:
        return 0.0  # non-Linux?
     # get VmKey line e.g. 'VmRSS:  9999  kB\n ...'
    i = v.index(VmKey)
    v = v[i:].split(None, 3)  # whitespace
    if len(v) < 3:
        return 0.0  # invalid format?
     # convert Vm value to bytes
    return float(v[1]) * _scale[v[2]]


def memory(since=0.0):
    '''Return memory usage in bytes.
    '''
    return _VmB('VmSize:') - since


def resident(since=0.0):
    '''Return resident memory usage in bytes.
    '''
    return _VmB('VmRSS:') - since


def stacksize(since=0.0):
    '''Return stack size in bytes.
    '''
    return _VmB('VmStk:') - since

import gc

print "GC state:", gc.isenabled()
gc.set_debug(gc.DEBUG_LEAK)

import osg2
osg2.osgInit([])

file_name = "/var/tmp/temp_file.osb"
osg2.SceneFileHandler.the().write(osg2.makeSphere(6,5), file_name)

nodes = []

for x in range(1000):
   #n = osg2.Node.create()
   #n = osg2.makeSphere(6,5)
   n = osg2.SceneFileHandler.the().read(file_name)
   
   #nodes.append(n)
   print "%s:  resident: %8.4f MB stack: %s KB"%(x, float(resident()/(1024.0*1024.0)),
                                           float(stacksize()/(1024.0)))
   sys.stdout.flush()
   #gc.collect()
